package com.ls.thread.xgCarthroughBridge;

public class Linght {
	
	
/*	public synchronized void go(){
		
		if(ling){
			
			int counter=4;
			while(counter>0){
				
				System.out.println("--绿灯---"+Thread.currentThread().getName()+"--过去第"+(5-counter)+"俩车");
				counter--;
				
			}
			//变灯
			ling=!ling;
		}else{
			
			System.err.println("----红灯---");
			int counter=2;
			while(counter>0){
				System.err.println("红灯还有"+counter+"秒");
				try {
					Thread.currentThread().sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				counter--;
			}
			//变灯
			ling=!ling;
			
		}
	}*/
	
	/*@SuppressWarnings("static-access")
	public synchronized void lv(){
		while(true){
			
		
		int counter=4;
		while(counter>0){
			
			
			System.out.println("--绿灯---"+Thread.currentThread().getName()+"--过去第"+(5-counter)+"俩车");
			try {
			counter--;
				
				Thread.currentThread().sleep(1000);
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		System.err.println("----红灯---");
		int ti=2;
		while(ti>0){
			System.err.println("红灯还有"+ti+"秒");
			try {
				Thread.currentThread().sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			ti--;
		}
		}
		
	}
	*/
	
	
	int counter=1;
	@SuppressWarnings("static-access")
	public synchronized boolean go(){
		
		try {
			notify();
			System.out.println("--绿灯---"+Thread.currentThread().getName()+"--过去第"+counter+"俩车");
			counter++;
			if(counter==4){
				return false;
				
			}
				Thread.currentThread().sleep(1000);
				
				//counter0开始才行
				if((counter-1)%4==0){
					int time=2;
					while(time > 0){
						System.out.println("红灯：还剩"+time+"秒");
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						time--;
					}
					
				}
				
				wait();
				
		} catch (InterruptedException e) {
				e.printStackTrace();
		}
		
		return true;
	}

}
